package com.ksyun.alert.utils;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * RequestId工具类
 * @author: hantou
 * @date: 2024-04-30
 */
@Component
public class RequestIdUtil {

    private static SnowflakeIdGenerateUtil snowflakeIdGenerateUtil;

    @Resource
    public void setSnowflakeIdGenerator(SnowflakeIdGenerateUtil snowflakeIdGenerateUtil) {
        RequestIdUtil.snowflakeIdGenerateUtil = snowflakeIdGenerateUtil;
    }

    private static final ThreadLocal<String> requestIdHolder = new ThreadLocal<>();

    /**
     * 生成RequestId
     */
    public static void generateRequestId() {
        requestIdHolder.set(String.valueOf(snowflakeIdGenerateUtil.nextId()));
    }

    /**
     * 生成RequestId
     * @param requestId RequestId
     */
    public static void generateRequestId(String requestId) {
        requestIdHolder.set(requestId);
    }

    /**
     * 获取RequestId
     * @return RequestId
     */
    public static String getRequestId() {
        return requestIdHolder.get();
    }

    /**
     * 移除RequestId
     */
    public static void removeRequestId() {
        requestIdHolder.remove();
    }
}